
// pages/notice/notice.js
const app = getApp()
Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    gradeArr: {
      type: Array,
      value: [],
    },
    registerObj: {
      type: Object,
      value: {}
    },
    gradeActiveIndex: {
      type: Number,
      value: 0
    },
    semesterIndex: {
      type: Number,
      value: 0
    },
    booksEditionIndex:{
      type: Number,
      value: 0
    }
  },
  data: {
    semesterArr: [
      {
        name: '上学期',
        value: '上'
      },
      {
        name: '下学期',
        value: '下'
      }
    ],
    booksEditions: [{
      name: '人教版PEP三起',
      hasCurrMiniApp: true,
      id: "1002",
      appId: 'wx086ab9dc3a4686da'
    },{
      name: '人教版新起点',
      hasCurrMiniApp: false,
      appId: 'wx78d0fd4c9bc18455',
      id: "1001",
    },
    {
      name: '新概念英语',
      hasCurrMiniApp: false,
      id: "1003",
      appId: 'wxb335664259de8410'
    },{
      name: '人教版精通',
      hasCurrMiniApp: false,
      appId: 'wx58c29661e6f88d8b',
      id: "1004",
    },    {
      name: '新概念英语（青少版）',
      hasCurrMiniApp: false,
      id: "1005",
      appId: 'wxbf6140c14ee1b85c'
    }],
    currSelectItems:''
  },
  methods: {    
  linkMiniapp(e) {
    var appid = e.target.dataset.appid
    var that = this;
    wx.showLoading({
      title: '跳转中···',
    })
    wx.navigateToMiniProgram({
      appId: appid,
      path: 'pages/bookList/bookList',
      extraData: {

      },
      envVersion: 'release', // develop	开发版	trial	体验版	release	正式版	
      success(res) {
        // 打开成功
        console.log("打开成功");
      },
      fail: function () {
        console.log("打开fail");
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },

    // 选择年级
    selectGrade(e) {
      var that = this;
      that.setData({
        gradeActiveIndex: e.target.dataset.index,
      })
    },

    // 选择学期
    selectSemester(e) {
      var that = this;
      that.setData({
        semesterIndex: e.target.dataset.index,
        currSelectItems:that.data.gradeArr[ e.target.dataset.index].gradeName+'('+that.data.semesterArr[e.target.dataset.index].value+')'
      })
    },

    // 选择版本
    selectBooksEdition(e){
      var that = this;
      var i = e.target.dataset.index;
      var item = this.data.booksEditions[i];
      if (item.hasCurrMiniApp) {
        that.setData({
          booksEditionIndex: i
        })
      } else {
        this.linkMiniapp(e);
      }
    },

    // 关闭
    myCloseGradeEdition(){
      var gradeName = "",semester = "";
      if(this.data.gradeActiveIndex > -1){
          gradeName = this.data.gradeArr[this.data.gradeActiveIndex].gradeName;
      }else{
        gradeName = "";
      }

      if(this.data.semesterIndex > -1){
        semester = this.data.semesterArr[this.data.semesterIndex].value;
      }else{
        semester = "";
      }
      var myEventDetail = {
        type:'test',
        gradeActiveIndex: this.data.gradeActiveIndex,
        semesterIndex: this.data.semesterIndex,
        booksEditionIndex:this.data.booksEditionIndex,
        currSelectItems:gradeName+'('+semester+')'
      }
      var myEventOption = { // 触发事件的选项
       
      }
      this.triggerEvent('myCloseGradeEdition', myEventDetail, myEventOption)
    },

    // 关闭
    reset(){
      var gradeName = "",semester = "";
      var myEventDetail = {
        type:'test',
        gradeActiveIndex: -1,
        semesterIndex: -1,
        booksEditionIndex:-1,
        currSelectItems:gradeName+'('+semester+')'
      }
      var myEventOption = { // 触发事件的选项
       
      }
      this.triggerEvent('myCloseGradeEdition', myEventDetail, myEventOption)
    }
  }
})